﻿
#include "TextArrayMetaHander.h"
#include <vtkAbstractArray.h>

namespace pst
{
    void TextArrayMetaHander::setArrayMeta(
        vtkAbstractArray* array, const TextArrayMetaStruct& meta)
    {
        if (array == nullptr)
        {
            return;
        }

        // 获取该列的 vtkInformation 对象
        vtkInformation* info = array->GetInformation();

        // 设置自定义的附属信息
        FILE_PATH()->Set(info, meta.filePath.toUtf8().constData());
        FILE_NAME()->Set(info, meta.fileName.toUtf8().constData());

        RESULT_PARA_TYPE()->Set(info, meta.resultParaType);
        RESULT_DATA_TYPE()->Set(info, meta.resultDataType);

        DATA_GROUP_NAME()->Set(info, meta.dataGroupName.toUtf8().constData());
        RELATED_SCALAR_INDEX()->Set(info, meta.relatedScalarIndex);
        RELATED_REALPART_INDEX()->Set(info, meta.relatedRealPartIndex);
        RELATED_IMAGPART_INDEX()->Set(info, meta.relatedImagPartIndex);
        RELATED_MAGPART_INDEX()->Set(info, meta.relatedMagPartIndex);
        RELATED_PHASEPART_INDEX()->Set(info, meta.relatedPhasePartIndex);

        UNIT()->Set(info, meta.unit.toUtf8().constData());
        KEY_VALUE_TYPE()->Set(info, meta.keyValueType);

    }

    bool TextArrayMetaHander::getArrayMeta(vtkAbstractArray* array, TextArrayMetaStruct& metaStruct)
    {
        //TextArrayMetaStruct metaStruct{};
        if (array == nullptr)
        {
            return false;
        }

        vtkInformation* infoRetrieved = array->GetInformation();

        if (FILE_PATH()->Has(infoRetrieved))
        {
            metaStruct.filePath = FILE_PATH()->Get(infoRetrieved);
        }

        if (FILE_NAME()->Has(infoRetrieved))
        {
            metaStruct.fileName = FILE_NAME()->Get(infoRetrieved);
        }

        if (RESULT_PARA_TYPE()->Has(infoRetrieved))
        {
            metaStruct.resultParaType = RESULT_PARA_TYPE()->Get(infoRetrieved);
        }

        if (RESULT_DATA_TYPE()->Has(infoRetrieved))
        {
            metaStruct.resultDataType = RESULT_DATA_TYPE()->Get(infoRetrieved);
        }

        if (DATA_GROUP_NAME()->Has(infoRetrieved))
        {
            metaStruct.dataGroupName = DATA_GROUP_NAME()->Get(infoRetrieved);
        }

        if (RELATED_SCALAR_INDEX()->Has(infoRetrieved))
        {
            metaStruct.relatedScalarIndex = RELATED_SCALAR_INDEX()->Get(infoRetrieved);
        }

        if (RELATED_REALPART_INDEX()->Has(infoRetrieved))
        {
            metaStruct.relatedRealPartIndex = RELATED_REALPART_INDEX()->Get(infoRetrieved);
        }

        if (RELATED_IMAGPART_INDEX()->Has(infoRetrieved))
        {
            metaStruct.relatedImagPartIndex = RELATED_IMAGPART_INDEX()->Get(infoRetrieved);
        }

        if (RELATED_MAGPART_INDEX()->Has(infoRetrieved))
        {
            metaStruct.relatedMagPartIndex = RELATED_MAGPART_INDEX()->Get(infoRetrieved);
        }

        if (RELATED_PHASEPART_INDEX()->Has(infoRetrieved))
        {
            metaStruct.relatedPhasePartIndex = RELATED_PHASEPART_INDEX()->Get(infoRetrieved);
        }

        if (UNIT()->Has(infoRetrieved))
        {
            metaStruct.unit = UNIT()->Get(infoRetrieved);
        }

        if (KEY_VALUE_TYPE()->Has(infoRetrieved))
        {
            metaStruct.keyValueType = KEY_VALUE_TYPE()->Get(infoRetrieved);
        }

        return true;
    }

    QString TextArrayMetaHander::getFilePathFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return QString();
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (FILE_PATH()->Has(infoRetrieved))
        {
            return QString::fromUtf8(FILE_PATH()->Get(infoRetrieved));
        }

        return QString();
    }

    QString TextArrayMetaHander::getFileNameFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return QString();
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (FILE_NAME()->Has(infoRetrieved))
        {
            return QString::fromUtf8(FILE_NAME()->Get(infoRetrieved));
        }

        return QString();
    }

    int TextArrayMetaHander::getResultParaTypeFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RESULT_PARA_TYPE()->Has(infoRetrieved))
        {
            return RESULT_PARA_TYPE()->Get(infoRetrieved);
        }

        return -1;
    }

    int TextArrayMetaHander::getResultDataTypeFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RESULT_DATA_TYPE()->Has(infoRetrieved))
        {
            return RESULT_DATA_TYPE()->Get(infoRetrieved);
        }

        return -1;
    }

    QString TextArrayMetaHander::getDataGroupNameFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (DATA_GROUP_NAME()->Has(infoRetrieved))
        {
            return QString::fromUtf8(DATA_GROUP_NAME()->Get(infoRetrieved));
        }

        return QString();
    }

    int TextArrayMetaHander::getRelatedScalarIndexFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RELATED_SCALAR_INDEX()->Has(infoRetrieved))
        {
            return RELATED_SCALAR_INDEX()->Get(infoRetrieved);
        }

        return -1;
    }

    int TextArrayMetaHander::getRelatedRealPartIndexFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RELATED_REALPART_INDEX()->Has(infoRetrieved))
        {
            return RELATED_REALPART_INDEX()->Get(infoRetrieved);
        }

        return -1;
    }

    int TextArrayMetaHander::getRelatedImagPartIndexFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RELATED_IMAGPART_INDEX()->Has(infoRetrieved))
        {
            return RELATED_IMAGPART_INDEX()->Get(infoRetrieved);
        }

        return -1;
    }

    int TextArrayMetaHander::getRelatedMagPartIndexFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RELATED_MAGPART_INDEX()->Has(infoRetrieved))
        {
            return RELATED_MAGPART_INDEX()->Get(infoRetrieved);
        }

        return -1;
    }

    int TextArrayMetaHander::getRelatedPhasePartIndexFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (RELATED_PHASEPART_INDEX()->Has(infoRetrieved))
        {
            return RELATED_PHASEPART_INDEX()->Get(infoRetrieved);
        }

        return -1;
    }

    QString TextArrayMetaHander::getUnitFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return QString();
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (UNIT()->Has(infoRetrieved))
        {
            return UNIT()->Get(infoRetrieved);
        }

        return QString();
    }

    int TextArrayMetaHander::getKeyValueTypeFromArray(vtkAbstractArray* array)
    {
        if (array == nullptr)
        {
            return -1;
        }

        vtkInformation* infoRetrieved = array->GetInformation();
        if (KEY_VALUE_TYPE()->Has(infoRetrieved))
        {
            return KEY_VALUE_TYPE()->Get(infoRetrieved);
        }

        return -1;
    }

    vtkInformationStringKey* TextArrayMetaHander::FILE_PATH()
    {
        // 使用 "FilePath" 作为键名，"TextArrayMetaHander" 作为所属组
        static vtkInformationStringKey* key = new vtkInformationStringKey("FilePath", "TextArrayMetaHander");
        return key;
    }

    vtkInformationStringKey* TextArrayMetaHander::FILE_NAME()
    {
        // 使用 "FileName" 作为键名，"TextArrayMetaHander" 作为所属组
        static vtkInformationStringKey* key = new vtkInformationStringKey("FileName", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RESULT_PARA_TYPE()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("ResultParaType", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RESULT_DATA_TYPE()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("ResultDataType", "TextArrayMetaHander");
        return key;
    }

    vtkInformationStringKey* TextArrayMetaHander::DATA_GROUP_NAME()
    {
        static vtkInformationStringKey* key = new vtkInformationStringKey("DataGroupName", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RELATED_SCALAR_INDEX()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("RelatedScalarIndex", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RELATED_REALPART_INDEX()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("RelatedRealPartIndex", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RELATED_IMAGPART_INDEX()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("RelatedImagPartIndex", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RELATED_MAGPART_INDEX()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("RelatedMagPartIndex", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::RELATED_PHASEPART_INDEX()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("RelatedPhasePartIndex", "TextArrayMetaHander");
        return key;
    }

    vtkInformationStringKey* TextArrayMetaHander::UNIT()
    {
        static vtkInformationStringKey* key = new vtkInformationStringKey("Unit", "TextArrayMetaHander");
        return key;
    }

    vtkInformationIntegerKey* TextArrayMetaHander::KEY_VALUE_TYPE()
    {
        static vtkInformationIntegerKey* key = new vtkInformationIntegerKey("KeyValueType", "TextArrayMetaHander");
        return key;
    }

    TextArrayMetaStruct::TextArrayMetaStruct(const QString& filePath, const QString& fileName,
        int resultParaType, int resultDataType, const QString& dataGroupNam,
        int relatedRealPartIndex, int relatedImagPartIndex, int relatedMagPartIndex,
        int relatedPhasePartIndex, int relatedScalarIndex, const QString& unit, int keyValueType)
    {
        this->filePath = filePath;
        this->fileName = fileName;
        this->resultParaType = resultParaType;
        this->resultDataType = resultDataType;
        this->dataGroupName = dataGroupNam;
        this->relatedRealPartIndex = relatedRealPartIndex;
        this->relatedImagPartIndex = relatedImagPartIndex;
        this->relatedMagPartIndex = relatedMagPartIndex;
        this->relatedPhasePartIndex = relatedPhasePartIndex;
        this->relatedScalarIndex = relatedScalarIndex;
        this->unit = unit;
        this->keyValueType = keyValueType;
    }
}

